package com.sgolub.commanndsonline.dao.entity;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sgolub.commanndsonline.domain.entity.UserOrder;

/**
 * @author sgolub
 *
 */
@Repository
public class UserOrderDAOImpl implements UserOrderDAO {

    @Autowired
    private SessionFactory sessionFactory;
    
	public UserOrder get(Integer id) {
		UserOrder userOrder = (UserOrder)sessionFactory.
								getCurrentSession().get(UserOrder.class, id);
		return userOrder;
	}

	public void add(UserOrder userOrder) {
		sessionFactory.getCurrentSession().save(userOrder);

	}

	public void update(UserOrder userOrder) {
		sessionFactory.getCurrentSession().update(userOrder);
	} 

	public void remove(Integer id) {
		sessionFactory.getCurrentSession().delete(get(id));
	}

	@SuppressWarnings("unchecked")
	public List<UserOrder> list() {
		return sessionFactory.getCurrentSession().createQuery("from UserOrder").list();
	}
	
	
	
	

	@SuppressWarnings("unchecked")
	public List<UserOrder> filter(String sortByColumn, String sortMode, 
								  HashMap<String, String> filtersMap) {
		
	   	System.out.println("== UserOrderDAOImpl.filter ==");
    	System.out.println("== PASSED FILTERS ==");
    	// Get a set of the entries 
    	Set set = filtersMap.entrySet(); 
    	// Get an iterator 
    	Iterator i = set.iterator(); 
    	// Display elements 
    	while(i.hasNext()) { 
	    	Map.Entry me = (Map.Entry)i.next(); 
	    	System.out.print(me.getKey() + ": "); 
	    	System.out.println(me.getValue()); 
    	} 
    	
		String sqlFrom = "from UserOrder";
		String sqlWhere = " where 1=1 ";
		String sqlOrder = "";
		String sql = "";
		
		if (sortByColumn.length()>0 & sortMode.length()>0){
			sqlOrder = " order by " + sortByColumn + " " + sortMode;
		}
    	

		System.out.println("== PARSED WHERE ==");
		if ( filtersMap.get("currUser").length() > 0 ){
			System.out.println(" and fk_manager_id =" + filtersMap.get("currUser"));
			sqlWhere+=" and fk_manager_id = " + filtersMap.get("currUser"); 
			}
		
		
//		if ( filtersMap.get("company").length() > 0 ){
//			System.out.println(" and upper(supportTypeName)  like upper('%" + filtersMap.get("supportType") + "%') ");
//			sqlWhere+=" and upper(supportTypeName)  like upper( '%" + filtersMap.get("supportType") + "%') "; 
//			}
		
		if ( filtersMap.get("orderStatus").length() > 0 && !filtersMap.get("orderStatus").equals("-1")){
			System.out.println(" and orderStatus  = " + filtersMap.get("orderStatus"));
			sqlWhere+=" and orderStatus  = " + filtersMap.get("orderStatus"); 
			}		
		
		if ( filtersMap.get("dateFrom").length() > 0 ){
			System.out.println(" and order_date  = " + filtersMap.get("dateFrom"));
			sqlWhere+=" and order_date  >= to_date('" + 
					filtersMap.get("dateFrom") + "', 'DD.MM.YYYY')"; 
			}
		
		if ( filtersMap.get("dateTo").length() > 0 ){
			System.out.println(" and order_date  = " + filtersMap.get("dateTo"));
			sqlWhere+=" and order_date  < to_date('" + 
					filtersMap.get("dateTo") + "', 'DD.MM.YYYY')"; 
			}		
		
		System.out.println("== SQL WHERE ==");
		System.out.println(sqlWhere);
    	
		
		sql = sqlFrom + sqlWhere + sqlOrder;
		
		System.out.println("Query = " + sql);
		
		Query queryH = sessionFactory.getCurrentSession().createQuery(sql);
		
		return queryH.list();
	}

	
	
	
	
	
	
	@SuppressWarnings("unchecked")
	public List<UserOrder> listCurrentUserOrder(Integer userId){
		
		String sql = "from UserOrder where fk_sec_user_id = " + userId;
		Query queryH = sessionFactory.getCurrentSession().createQuery(sql);
		List<UserOrder> userOrderList = queryH.list();	
		return userOrderList;
	}
	
	public List<UserOrder> listCurrentManagerOrder(Integer userId){
		
		String sql = "from UserOrder where fk_manager_id = " + userId;
		Query queryH = sessionFactory.getCurrentSession().createQuery(sql);
		List<UserOrder> userOrderList = queryH.list();	
		return userOrderList;
	}
	
}
